package com.goldgov.jsondoc.doc;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.goldgov.jsondoc.revsoler.JsonInfo;
import com.goldgov.jsondoc.revsoler.OperateInfo;
import com.goldgov.jsondoc.revsoler.ParametersInfo;

public class GenerateWordByPOI {
	
	private static XWPFDocument document= new XWPFDocument();
	
	public static SerializeConfig mapping = new SerializeConfig(); 
	
	public static void generateDocument(OutputStream out,JsonInfo jsonInfo) throws IOException {
		mapping.setAsmEnable(true);
		if(jsonInfo == null) {
			return;
		}
		//添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居左
        titleParagraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText(jsonInfo.getInfo().getDescription());
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(20);
        
        LinkedHashMap<String, LinkedHashMap<String, OperateInfo>> paths = jsonInfo.getPaths();
        
        Set<String> keySet = paths.keySet();
        for(String path : keySet) {
        	LinkedHashMap<String, OperateInfo> requestTypeMap = paths.get(path);
        	Set<String> requestTypeSet = requestTypeMap.keySet();
        	for(String requestType : requestTypeSet) {
        		OperateInfo operateInfo = requestTypeMap.get(requestType);
        		createTables(path, requestType, operateInfo);
        	}
        }
        
        document.write(out);
        out.close();
	}
	
	private static void createTables(String path,String requestType,OperateInfo operateInfo) {
		XWPFTable infoTable = document.createTable();
        
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
        
        XWPFTableRow interfaceDescRow = infoTable.getRow(0);
        interfaceDescRow.getCell(0).setText("接口描述");
        interfaceDescRow.addNewTableCell().setText(operateInfo.getSummary());

        XWPFTableRow requestTypeRow = infoTable.createRow();
        requestTypeRow.getCell(0).setText("请求方式");
        requestTypeRow.getCell(1).setText(requestType);
        
        
        String paramStr = "";
        List<ParametersInfo> parameters = operateInfo.getParameters();
        if(parameters != null) {
        	for(ParametersInfo pinfo : parameters) {
        		paramStr += pinfo.getName() + " " + (pinfo.getDescription() == null ? "" : pinfo.getDescription()) + " " + pinfo.getType() + "\r\n";
        	}
        }
        XWPFTableRow paramDefineRow = infoTable.createRow();
        paramDefineRow.getCell(0).setText("参数定义");
        paramDefineRow.getCell(1).setText(paramStr);

        XWPFTableRow inputUrlRow = infoTable.createRow();
        inputUrlRow.getCell(0).setText("输入URL");
        inputUrlRow.getCell(1).setText(path + "/" + operateInfo.getOperationId());

        XWPFTableRow returnValueRow = infoTable.createRow();
        returnValueRow.getCell(0).setText("返回值");
        
		JSONObject parseObject = JSON.parseObject(operateInfo.getResponses(), Feature.AutoCloseSource);
		
        returnValueRow.getCell(1).setText("responses : " + new JsonFormat().format(parseObject, 2,false));

        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun paragraphRun = paragraph.createRun();
        paragraphRun.setText("\r");
	}
	
}
